package application.filter;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class PermaLinkFilter implements Filter
{
	
	// System.out.println("command = " + command);
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException 
	{
		String command = getCommandFromPathInfoAndMaybeSetSomeAttributes((HttpServletRequest)request);
		request.setAttribute("command", command);
		chain.doFilter(request, response);
	}
	
	
	public static enum Commands 
	{
		login, browsegroups, logout, acceptinvite, addsingleuser, adduser, browseinvites, browserequests, creategroup, editgroup, inviteuser, requestinvite, viewgroup, removemember,
		removegroup, home
	}

	private String findCommand(String path, HttpServletRequest request)
	{
		Commands cmd = Commands.valueOf(path.substring(1));
		request.setAttribute("currentCommand", cmd);
		String finalCmd = null;
		
		switch(cmd)
		{
			case login:
				finalCmd = "Login";
				break;
			case browsegroups:
				finalCmd = "BrowseGroups";
				break;
			case logout:
				finalCmd = "Logout";
				break;
			case acceptinvite:
				finalCmd = "AcceptDeclineInvite";
				break;
			case addsingleuser:
				finalCmd = "AddSingleUser";
				break;
			case adduser:
				finalCmd = "AddUser";
				break;
			case browseinvites:
				finalCmd = "BrowseInvites";
				break;
			case browserequests:
				finalCmd = "BrowseRequests";
				break;
			case creategroup:
				finalCmd = "CreateGroup";
				break;
			case editgroup:
				finalCmd = "EditGroup";
				break;
			case inviteuser:
				finalCmd = "InviteUser";
				break;
			case requestinvite:
				finalCmd = "RequestInvite";
				break;
			case viewgroup:
				finalCmd = "ViewCurrentGroup";
				break;
			case removemember:
				finalCmd = "RemoveMember";
				break;
			case removegroup:
				finalCmd = "RemoveGroup";
				break;
			case home:
			default:
				finalCmd = "home";
				break;
		}
		
		return finalCmd;
	}
	
	/**
	 * Attempt to extract the command from the request's pathInfo. Note that
	 * this method might set request attributes.
	 * 
	 * @param request
	 * @return null or the command as a simple class name (i.e. not fully
	 *         qualified with the package).
	 */
	private String getCommandFromPathInfoAndMaybeSetSomeAttributes(
			HttpServletRequest request) 
	{
		String path = request.getPathInfo();
		//System.out.println("path = " + path);
		
		if (path == null)
			return null;

		String command = findCommand(path, request);
		
//		Pattern pattern = Pattern.compile("^/(login|browsegroups)");
//		Matcher matcher = pattern.matcher(path);
//		if (matcher.find()) 
//		{			
//			command = "login".equals(matcher.group(1)) ? "Login"
//					: "BrowseGroups";
//			
//			//id = matcher.group(2);
//			//request.setAttribute("id", id);
//		}
	
		// System.out.println("command = " + command);
		return command;
	}
	
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	
}
